package com.feinno.module.security.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feinno.framework.common.web.AbstractJQueryEntityController;
import com.feinno.framework.common.web.support.JsonListResult;
import com.feinno.framework.common.web.support.JsonResult;
import com.feinno.module.security.SecurityConstants;
import com.feinno.module.security.domain.Menu;
import com.feinno.module.security.domain.Resource;
import com.feinno.module.security.service.MenuService;
import com.feinno.module.security.service.ResourceService;
import com.google.common.collect.Maps;

@Controller
@RequestMapping(value = "/manage/system/resource")
public class JsonResourceController extends
		AbstractJQueryEntityController<Resource, ResourceService> {

	@Autowired
	ResourceService resourceService;

	@Autowired
	MenuService menuService;

	@Override
	protected Map<String, Boolean> getSortMap(HttpServletRequest request) {
		Map<String, Boolean> sortMap = Maps.newLinkedHashMap();
		sortMap.put("menu.orderTime", false);
		sortMap.put("orderTime", false);
		return sortMap;
	}


	@RequestMapping(value = { "listAllJson" })
	@ResponseBody
	public JsonListResult<Resource> listAllJson(HttpServletRequest request,
			HttpServletResponse response) {
		JsonListResult<Resource> result = new JsonListResult<Resource>();
		try {
			result.appendData(referenceData(request));
			List<Resource> resources = doQuery(request, response);
			result.setTotal(Long.valueOf(resources.size()));
			result.setRows(resources);
		} catch (Exception e) {
			handleException(result, "查询所有数据", e);
		}
		return result;
	}

	@RequestMapping(value = "moveTop")
	@ResponseBody
	public JsonResult moveTop(HttpServletRequest request,
			HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try {
			String id = request.getParameter(getEntityIdName());
			resourceService.moveTop(Long.valueOf(id));
			result.setMessage("置顶成功");
		} catch (Exception e) {
			handleException(result, "置顶", e);
		}
		return result;
	}

	@RequestMapping(value = "moveUp")
	@ResponseBody
	public JsonResult moveUp(HttpServletRequest request,
			HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try {
			String id = request.getParameter(getEntityIdName());
			resourceService.moveUp(Long.valueOf(id));
			result.setMessage("上移成功");
		} catch (Exception e) {
			handleException(result, "上移", e);
		}
		return result;
	}

	@Override
	protected Resource onSave(HttpServletRequest request,
			HttpServletResponse response, Model model, Resource entity,
			boolean isCreate) throws Exception {
		String menuId = request.getParameter("cbmenuid");
		if (StringUtils.isNotBlank(menuId)) {
			Menu menu = menuService.get(Long.valueOf(menuId));
			entity.setMenu(menu);
		} else {
			entity.setMenu(null);
		}

		return super.onSave(request, response, model, entity, isCreate);
	}

	@Override
	protected void referenceData(HttpServletRequest request,
			Map<String, Object> model) {
		try {
			List<Menu> menus = menuService.getAll();
			model.put("menus", menus);
		} catch (Exception e) {
			// ig
		}
		model.put("allIcons", SecurityConstants.FRAMEWORK_ALL_ICONS);
		model.put("allShowStates", SecurityConstants.SHOW_STATE_MAPPING);
		model.put("allShowModes", SecurityConstants.SHOW_MODE_MAPPING);
	}

}
